package com.homedo.distributed.id.client.gen.buffer;

import com.google.common.collect.Queues;
import lombok.Data;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

@Data
public class Gen<T> {
    private ConcurrentLinkedQueue<T> buffer = Queues.newConcurrentLinkedQueue();
    private volatile boolean isReady;//是否可用
    private volatile boolean isRunning;//是否使用当中的

    private volatile int capacity;//总容量
    private AtomicInteger used = new AtomicInteger(0);//容量使用量

    public T gen() {
        T gen = buffer.poll();
        if (gen != null) {
            this.plusUsed();
        }
        return gen;
    }

    public boolean putBufEle(T e) {
        return buffer.offer(e);
    }

    private void plusUsed() {
        used.getAndIncrement();
    }

    public int size() {
        return buffer.size();
    }

    public void resetUsed() {
        this.used.set(0);
    }
}
